var userEditParams = {
    action:{
        addUser:"sysUser/insertUserVo",
        findRoleName:"role/selectRoleName",
        findUserById:"sysUser/selectUserVoByUserId/",
        updateUser: "sysUser/updateUserVo"
    }
}
$(function () {
    findRoleName();
    $("#btnSubmit").click(function () {
        addUser();
    });
    new uploadPreview({
        UpBtn:"tempFile",
        DivShow:"imgGroup",
        ImgShow:"imgPreview"
    });
    var user_id = baseUtilParams.GetQueryString("user_id");
    if(user_id!=null){
        findUserById(user_id);
    };
    $("#btnSubmitUpdate").click(function () {
        updateUser();
    });
});
/**
 * @title:<h3> 新增用户 <h3>
 * @author: loy
 * @date: 2018/10/23 18:03
 * @params
 * @return
 **/
function addUser() {
    if (checkStr()){
        var data = $("#formSubmit").serialize();
        //requestUtilParams.xhr(userEditParams.action.addUser,data).done(function (rs) {
            requestUtilParams.xhrUpload(userEditParams.action.addUser,"#formSubmit").done(function (rs) {
                alert(rs.message);
                $("#formSubmit")[0].reset();
            });
    }
}
/**
 * @title:<h3> 检查账号和姓名是否都输入 <3>
 * @author: loy
 * @date: 2018/10/23 18:25
 * @params
 * @return
 **/
function checkStr() {
    var str = "";
    var uid=$("#account_user_submit").val();
    var uname = $("#name_user_submit").val();
    if(uid==null&&uname==null){
        str +="账号和姓名不能为空/n"
    }
    if(str.length>0){
        alert(str);
        return false;
    }else {
        var num=0;
        $("#roleGroup input:checked").each(function () {
            $(this).attr("name","listUserRole["+num+"].fk_role_id");
            num++;
        });
    }
    return true;
}
/*
* @title:<h3> 显示用户权限值 <h3>
* @author: loy
* @date: 2018/10/24 18:08
* @params
* @return
**/
function findRoleName() {
    requestUtilParams.xhrGet(userEditParams.action.findRoleName,{async:false}).done(function (rs) {
            $("#roleGroup").empty();
            var roleName = rs.data;
            for(var i=0;i<roleName.length;i ++){
                $("#roleGroup").append("<input type='checkbox' title='"+roleName[i].name+"' value='"+roleName[i].role_id+"'>")
            }
        layui.use("form",function () {
            var form=layui.form;
            form.render();
        });
    });
};
/**
 * @title:<h3> 显示用户信息 <h3>
 * @author: loy
 * @date: 2018/10/24 19:30
 * @params
 * @return
 **/
function findUserById(user_id) {
    requestUtilParams.xhrGet(userEditParams.action.findUserById+user_id).done(function (rs) {
        var user = rs.data.user;
        var listUserRole = rs.data.listUserRole;
        $("#name_user_submit").val(user.name);
        $("#account_user_submit").val(user.account);
        $("#memo_user_submit").val(user.memo);
        $("#version_user_submit").val(user.version);
        $("#user_id_user_submit").val(user.user_id);
        $("#phone_user_submit").val(user.phone);
        $("#birthday_user_submit").val(baseUtilParams.simpDataFormat(user.birthday,'yyyy-MM-dd'));
        //显示性别，先将原有的选中的去掉
        //$("#sexGroup input:checked").prop("checked",false).next(".layui-form-radio").removeClass("layui-form-radioed");
        $("#sexGroup input[value='"+user.sex+"']").next(".layui-form-radio").click();
        for (var i = 0; i < listUserRole.length; i++) {
            $("#roleGroup input[value='"+listUserRole[i].fk_role_id+"']").prop("checked",true).next(".layui-form-checkbox").addClass("layui-form-checked");
        }
        if(rs.data.fileLog!=null){
            $("#imgPreview").attr("src",requestUtilParams.host+"files/upload/"+rs.data.fileLog.save_path);
        }
        $("#btnCancel").click();
    });
};

function updateUser() {
    if (checkStr()){
        requestUtilParams.xhrUpload(userEditParams.action.updateUser,"#form_submit").done(function (rs) {
            alert(rs.message);
            $("#btnClose").click();
        })
    }
}
